package com.firmboy.jdbc.hello.dao;

import com.firmboy.jdbc.hello.model.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * dao层实现类
 *
 * @author firmboy
 * @create 2018-03-12 上午11:23
 **/
@Repository
public class AccountDaoImpl {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void add(Account account){
        jdbcTemplate.update("INSERT INTO account (name,money) VALUES (?,?)", account.getName(),account.getMoney());
    }



    public Account findAccountById(int id){
        String sql = "select * from account where id = ?";
        List<Account> list =jdbcTemplate.query(sql,new Object[]{id},new BeanPropertyRowMapper(Account.class));
        if(list!=null && list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }

    public List<Account> getAccounts(){
        List list = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper(Account.class));
        if(list!=null && list.size()>0){
            return list;
        }else{
            return null;
        }

    }

}
